//
//  SAOperatingDocumentsDetailCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/5/7.
//

#import "SAOperatingDocumentsDetailCell.h"
#import "SASalesInvoiceDetailModel.h"

@interface SAOperatingDocumentsDetailCell ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *colourSizeLab;
@property (nonatomic, strong) UILabel *numLab;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UILabel *discountPriceLab;
@property (nonatomic, strong) UILabel *percentageLab;
@property (nonatomic, strong) UILabel *totalPriceLab;

@property (nonatomic, strong) UILabel *returnLab;

@end

@implementation SAOperatingDocumentsDetailCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASaleSkuListModel *)model {
    _model = model;
    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",model.colourName,model.sizeName];
    self.numLab.text = [NSString stringWithFormat:@"%@/%@",model.count,[NSString stringWithFormat:@"%0.2f",[model.price floatValue]/100.f].getEffectivePointString];
    CGFloat discountRate = [model.discountRate floatValue]/100;
    if (discountRate == 100) {
        self.totalPriceLab.frame = SARectMake(163, 0, 70, 40);
        self.percentageLab.text = @"";
        NSString *textContent = [NSString stringWithFormat:@"%0.2f",[model.totalPrice floatValue]/100.f].getEffectivePointString;
        self.totalPriceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:nil];
    }else {
        self.percentageLab.text = [NSString stringWithFormat:@"%0.2f%%",discountRate];
        self.totalPriceLab.frame = SARectMake(163, 20, 70, 15);
        NSString *textContent = [NSString stringWithFormat:@"%0.2f",[model.totalPrice floatValue]/100.f].getEffectivePointString;
        NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
        textDict[NSStrikethroughColorAttributeName] = kPinkColor;
        textDict[NSForegroundColorAttributeName] = kBlackColor;
        textDict[NSFontAttributeName] = kFont(13);
        textDict[NSStrikethroughStyleAttributeName] = [NSNumber numberWithInteger:NSUnderlineStyleSingle];
        textDict[NSBaselineOffsetAttributeName] = @(NSUnderlineStyleSingle);
        self.totalPriceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:textDict];
    }
    
    self.discountPriceLab.text = [NSString stringWithFormat:@"%0.2f",[model.discountPrice floatValue]/100.f].getEffectivePointString;

    NSInteger returnCount = [model.returnCount integerValue];
    NSInteger count = [model.count integerValue];
    if (model.salesCount.length > 0) {
        count = [model.salesCount integerValue];
    }
    if (returnCount > 0) {
        self.returnLab.hidden = NO;
        self.discountPriceLab.frame = SARectMake(234, 5, 70, 15);
        if (returnCount < count) {
            self.returnLab.text = @"部分退";
        }else {
            self.returnLab.text = @"已退款";
        }
    }else {
        self.returnLab.hidden = YES;
        self.discountPriceLab.frame = SARectMake(234, 0, 70, 40);
    }
    
    self.priceLab.text = [NSString stringWithFormat:@"%0.2f",[model.profit floatValue]/100.f].getEffectivePointString;
}

-(void)initUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.colourSizeLab];
    [self.contentView addSubview:self.numLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.discountPriceLab];
    [self.contentView addSubview:self.percentageLab];
    [self.contentView addSubview:self.totalPriceLab];
    [self.contentView addSubview:self.returnLab];
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(20, 0, 60, 40)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(15);
        _titleLab.adjustsFontSizeToFitWidth = YES;
        _titleLab.minimumScaleFactor = 0.8;
    }
    return _titleLab;
}

-(UILabel *)numLab {
    if (!_numLab) {
        _numLab = [[UILabel alloc] initWithFrame:SARectMake(81, 0, 81, 40)];
        _numLab.textColor = kBlackColor;
        _numLab.font = kFont(15);
        _numLab.textAlignment = NSTextAlignmentCenter;
        _numLab.adjustsFontSizeToFitWidth = YES;
        _numLab.minimumScaleFactor = 0.8;
    }
    return _numLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:SARectMake(305, 0, 70, 40)];
        _priceLab.textColor = kBlackColor;
        _priceLab.font = kFont(15);
        _priceLab.textAlignment = NSTextAlignmentCenter;
        _priceLab.adjustsFontSizeToFitWidth = YES;
        _priceLab.minimumScaleFactor = 0.8;
    }
    return _priceLab;
}

-(UILabel *)percentageLab {
    if (!_percentageLab) {
        _percentageLab = [[UILabel alloc] initWithFrame:SARectMake(163, 5, 70, 15)];
        _percentageLab.textColor = kPinkColor;
        _percentageLab.font = kFont(15);
        _percentageLab.textAlignment = NSTextAlignmentCenter;
        _percentageLab.adjustsFontSizeToFitWidth = YES;
        _percentageLab.minimumScaleFactor = 0.8;
    }
    return _percentageLab;
}

-(UILabel *)totalPriceLab {
    if (!_totalPriceLab) {
        _totalPriceLab = [[UILabel alloc] initWithFrame:SARectMake(163, 0, 70, 40)];
        _totalPriceLab.textColor = kBlackColor;
        _totalPriceLab.font = kFont(15);
        _totalPriceLab.textAlignment = NSTextAlignmentCenter;
        _totalPriceLab.adjustsFontSizeToFitWidth = YES;
        _totalPriceLab.minimumScaleFactor = 0.8;
    }
    return _totalPriceLab;
}

-(UILabel *)discountPriceLab {
    if (!_discountPriceLab) {
        _discountPriceLab = [[UILabel alloc] initWithFrame:SARectMake(234, 0, 70, 40)];
        _discountPriceLab.textColor = kBlackColor;
        _discountPriceLab.font = kFont(15);
        _discountPriceLab.textAlignment = NSTextAlignmentCenter;
        _discountPriceLab.adjustsFontSizeToFitWidth = YES;
        _discountPriceLab.minimumScaleFactor = 0.8;
    }
    return _discountPriceLab;
}

-(UILabel *)returnLab {
    if (!_returnLab) {
        _returnLab = [[UILabel alloc] initWithFrame:SARectMake(234, 20, 70, 15)];
        _returnLab.textColor = kPinkColor;
        _returnLab.font = kFont(13);
        _returnLab.textAlignment = NSTextAlignmentCenter;
        _returnLab.text = @"已退款";
        _returnLab.adjustsFontSizeToFitWidth = YES;
        _returnLab.minimumScaleFactor = 0.8;
    }
    return _returnLab;
}

@end
